package com.zjhd.sealcutting.service.impl;

import com.google.common.collect.Maps;
import com.zjhd.sealcutting.dao.UserInfoMapper;
import com.zjhd.sealcutting.domain.UserInfo;
import com.zjhd.sealcutting.param.UserSearchQuery;
import com.zjhd.sealcutting.vo.SearchResult;
import com.zjhd.sealcutting.service.UserInfoService;
import com.zjhd.sealcutting.utils.CookieUtil;
import com.zjhd.sealcutting.utils.DateUtil;
import com.zjhd.sealcutting.utils.TokenUtil;
import com.zjhd.sealcutting.utils.response.ResponseResult;
import com.zjhd.sealcutting.utils.response.ResultGenerator;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * TODO  用户管理
 *
 * @author wangchao
 * @version 1.0
 * @date 2020/11/23 13:39
 */
@Service
public class UserInfoServiceImpl implements UserInfoService {

    @Autowired
    private UserInfoMapper userInfoMapper;

    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    /**
     * TODO  分页查询用户列表
     * @author wangchao
     * @date 2020/11/23 14:28
     * @param
     * @return
     */
    @Override
    public SearchResult queryUserParamlist(UserSearchQuery param) throws Exception {
        //定义map 封装查询参数到map中
        Map<String, Object> map = Maps.newLinkedHashMap();
        if(StringUtils.isNotBlank(param.getUserName())){
            map.put("userName",param.getUserName());
        }
        if(StringUtils.isNotBlank(param.getMobile())){
            map.put("mobile",param.getMobile());
        }
        if(param.getState()!=null){
            map.put("state",param.getState());
        }
        //当前页  分页数
        map.put("pageIndex", (Integer.parseInt(param.getPageIndex()) - 1) * Integer.parseInt(param.getPageSize()));
        map.put("pageSize", Integer.parseInt(param.getPageSize()));
        //查询数据
        List<UserInfo> userInfos=userInfoMapper.queryParamUserList(map);
        long totalCount=userInfoMapper.queryParamUserListCount(map);
        if(userInfos.size()>0){
            return new SearchResult.Builder<List<UserInfo>>()
                    .setCurrent(Integer.parseInt(param.getPageIndex()))
                    .setPageSize(Integer.parseInt(param.getPageSize()))
                    .setTotalCount(totalCount)
                    .setPagedItems(userInfos == null ? new ArrayList() : userInfos)
                    .build();
        }else {
            return new SearchResult.Builder().setPagedItems(new ArrayList()).build();
        }
    }

    /**
     * TODO  新增用户信息
     * @author wangchao
     * @date 2020/11/23 15:31
     * @param
     * @return
     */
    @Override
    @Transactional
    public ResponseResult insertUser(UserInfo userInfo) throws Exception {
        try {
            userInfo.setState(0);
            userInfo.setCreateTime(DateUtil.getCurrentTime());
            int i=userInfoMapper.insert(userInfo);
            if(i>0){
              return ResultGenerator.genSuccessResult("新增用户成功!");
            }else {
              return ResultGenerator.genFailResult("新增用户失败!");
            }
        }catch (Exception ex){
            ex.printStackTrace();
        }
        return ResultGenerator.genFailResult("新增用户失败!");
    }

    /**
     * TODO  修改用户信息
     * @author wangchao
     * @date 2020/11/23 15:31
     * @param
     * @return
     */
    @Override
    @Transactional
    public ResponseResult updateUser(UserInfo userInfo) throws Exception {
        try {
            int i=userInfoMapper.updateByPrimaryKeySelective(userInfo);
            if(i>0){
                return ResultGenerator.genSuccessResult("修改用户成功!");
            }else {
                return ResultGenerator.genFailResult("修改用户失败!");
            }
        }catch (Exception ex){
            ex.printStackTrace();
        }
        return ResultGenerator.genFailResult("修改用户失败!");
    }

    /**
     * TODO  查询用户信息
     * @author wangchao
     * @date 2020/11/23 15:31
     * @param
     * @return
     */
    @Override
    public UserInfo queryUserInfo(Integer id) throws Exception {
        return userInfoMapper.selectByPrimaryKey(id);
    }

    /**
     * TODO  禁用用户
     * @author wangchao
     * @date 2020/11/23 16:17
     * @param
     * @return
     */
    @Override
    @Transactional
    public boolean disableOrEnableUser(Integer id,Integer type) throws Exception {
        boolean bool=false;
        if(type==0){
            bool=userInfoMapper.updateByIdState(id,type);
        }
        if(type==1){
            bool=userInfoMapper.updateByIdState(id,type);
        }
        return bool;
    }

    /**
     * TODO  获取当前用户信息
     * @author wangchao
     * @date 2020/11/24 15:37
     * @param
     * @return
     */
    @Override
    public UserInfo queryCurrentUserInfo(HttpServletRequest request, HttpServletResponse response) throws Exception {
        String authorization = CookieUtil.get(request,"Authorization").getValue();
        UserInfo userInfo=new UserInfo();
        if(StringUtils.isNotBlank(authorization)){
            userInfo= TokenUtil.parseAccessToken(authorization);
        }
        return userInfo;
    }

    /**
     * TODO  修改用户图像
     * @author wangchao
     * @date 2020/11/24 18:25
     * @param
     * @return
     */
    @Override
    @Transactional
    public ResponseResult updateByIdPortrait(UserInfo userInfo)throws Exception {
        //定义map 封装查询参数到map中
        Map<String, Object> map = Maps.newLinkedHashMap();
        map.put("id",userInfo.getId());
        map.put("portrait",userInfo.getPortrait());
        boolean bool=userInfoMapper.updateByIdPortrait(map);
        if(bool){
            return ResultGenerator.genSuccessResult("用户图像修改成功");
        }else {
            return ResultGenerator.genFailResult("用户图像修改失败");
        }
    }

    /**
     * TODO  修改用户信息
     * @author wangchao
     * @date 2020/11/25 10:30
     * @param
     * @return
     */
    @Override
    @Transactional
    public ResponseResult updateByIdUserInfo(UserInfo record) throws Exception {
        boolean bool=userInfoMapper.updateByIdUserInfo(record);
        if(bool){
            return ResultGenerator.genSuccessResult("用户信息修改成功");
        }else {
            return ResultGenerator.genFailResult("用户信息修改失败");
        }
    }

    /**
     * TODO 根据手机号和验证码判断验证码是否正确
     * @author wangchao
     * @date 2020/11/27 16:38
     * @param
     * @return
     */
    @Override
    public boolean isVerificationCode(String mobile, String code) throws Exception {
        boolean bool=false;
        //从redis中取出验证码
        String verificationCode=stringRedisTemplate.opsForValue().get(mobile+"-"+code);
        if(StringUtils.isNotBlank(verificationCode) && verificationCode.equals(code)){
            bool=true;
        }
        return bool;
    }

    /**
     * TODO  验证手机号是否存在
     * @author wangchao
     * @date 2020/12/2 15:53
     * @param
     * @return
     */
    @Override
    public boolean isMobile(String mobile) throws Exception {
        boolean bool=false;
        int i=userInfoMapper.queryByMobileCount(mobile);
        if(i>0){
            bool=true;
        }
        return bool;
    }

    /**
     * TODO 查询企业集合
     * @author wangchao
     * @date 2020/12/10 15:56
     * @param
     * @return
     */
    @Override
    public List<String> queryGroupByCompany() throws Exception {
        return userInfoMapper.queryGroupByCompany();
    }


    /**
     * TODO  根据企业查询用户
     * @author wangchao
     * @date 2020/12/10 15:58
     * @param
     * @return
     */
    @Override
    public List<UserInfo> queryByCompanyUser(String company) throws Exception {
        return userInfoMapper.queryByCompanyUser(company);
    }

    /**
     * TODO  查询所有启用用户
     * @author wangchao
     * @date 2020/12/16 9:59
     * @param
     * @return
     */
    @Override
    public List<UserInfo> queryAllUser() throws Exception {
        return userInfoMapper.queryAllUser();
    }

}
